package symbolrt.synthesis;

import symbolrt.SYMBOLRT;
import symbolrt.base.TIOSTS;
import symbolrt.translation.parser.TranslationUnit;
import symbolrt.translation.parser.base.AbstractTIOSTS;

/**
 * <code>DataManager</code> Class. <br>
 * 
 * This class is responsible for all compilation process.
 * 
 * @author Jeanderson Barros Candido ( <a
 *         href="mailto:jeandersonbc@gmail.com">jeandersonbc@gmail.com</a> )
 * 
 * @version 1.0 <br>
 *          (C) Copyright 2012 SYMBOL-RT Translator.
 */
public class SynthesisUnit {

	private static String specName = null;
	private static String tpName = null;
	private static Builder builder = null;

	/**
	 * Sets the required data to initialize the compilation process
	 * 
	 * @param specificationName
	 * @param testPurposeName
	 */
	public static void setUp(String specificationName, String testPurposeName) {
		SynthesisUnit.specName = specificationName;
		SynthesisUnit.tpName = testPurposeName;
		builder = new Builder();
	}

	/**
	 * Initializes the compilation process
	 */
	public static void init() {
		if (checkSetUp()) {
			System.out.println("SyMBolRT: Starting compilation process");
			AbstractTIOSTS spec = TranslationUnit.getModels().get(specName);
			AbstractTIOSTS tp = TranslationUnit.getModels().get(tpName);
			builder.init(spec, tp);
			try {
				SYMBOLRT.getInstance().generateTestCases(getProcessedSpec(),
						getProcessedTP(), true);
			} catch (Exception e) {
				System.err
						.println("SyMBolRT: An error occurred in test case generation process");
			}
		}
	}

	/**
	 * Checks setup parameters
	 * 
	 * @return <code>true</code> if test purpose and specification were found
	 */
	private static boolean checkSetUp() {
		boolean specModelFound = false;
		boolean tpModelFound = false;

		if (TranslationUnit.getModels() == null) {
			System.out.println("Error: There are no models processed");
		} else {
			for (AbstractTIOSTS m : TranslationUnit.getModels().values()) {
				if (m.getName().equals(specName)) {
					specModelFound = true;
				}
				if (m.getName().equals(tpName)) {
					tpModelFound = true;
				}
			}
			if (!specModelFound)
				System.out.println("SyMBolRT: specification '" + specName
						+ "' not found");
			if (!tpModelFound)
				System.out.println("SyMBolRT: test purpose '" + tpName
						+ "' not found");
		}

		return (specModelFound && tpModelFound);
	}

	/**
	 * @return The processed TIOSTS for test purpose
	 */
	public static TIOSTS getProcessedTP() {
		return builder.getProcessedTP();
	}

	/**
	 * @return The processed TIOSTS for system specification
	 */
	public static TIOSTS getProcessedSpec() {
		return builder.getProcessedSpec();
	}

}
